<?php
/** ************************
	Php Quran - 2009
Copyrights for all muslims
***************************/

		
	// not for directly open
	if (!defined('IN_INDEX'))
	{
		exit('no directly opening : ' . __file__);
	}

	//we are in the common file 
	define ('IN_COMMON', true);
	
	//
	//development stage;  developers stage
	// 
	define('DEV_STAGE', true);
		 
	// Report all errors, except notices
	defined('DEV_STAGE') ? @error_reporting(E_ALL) : @error_reporting(E_ALL ^ E_NOTICE);
	//Just to check
	define('IN_PHP6', (version_compare(PHP_VERSION, '6.0.0-dev', '>=') ? true : false));
				
	// start session
	$s_time = 86400 * 2; // 2 : two days 
	$s_key = (!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') . (!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null);
	$s_sid = 'quran_' . substr('_' . md5($s_key), 0, 8);
	@ini_set('session.use_only_cookies', false);
	@ini_set('session.auto_start', false);
	//this will help people with some problem with their sessions path
	//session_save_path('./cache/');	
	session_name($s_sid);
	@session_start();


	function stripslashes_our(&$value)
	{
		return is_array($value) ? array_map('stripslashes_our', $value) : stripslashes($value);  
	} 
	
	//unsets all global variables set from a superglobal array
	function unregister_globals() 
	{ 
		foreach (func_get_args() as $name)
		{
			foreach ($GLOBALS[$name] as $key=>$value)
			{
				if (isset($GLOBALS[$key]))
				{
					unset($GLOBALS[$key]);
				}
			}
		}
	}

	if (@ini_get('register_globals'))
	{
		unregister_globals('_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES'); 
	}
	
	if( (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) || 
		(@ini_get('magic_quotes_sybase') && (strtolower(@ini_get('magic_quotes_sybase')) != "off")) )
	{ 
		stripslashes_our($_GET); 
		stripslashes_our($_POST); 
		stripslashes_our($_COOKIE); 
	}
		
	
	//time of start and end and wutever
	function get_microtime()
	{
		list($usec, $sec) = explode(' ', microtime());	return ((float)$usec + (float)$sec);
	}
	
	$starttm = get_microtime();
	
	//path 
	if(!defined('PATH'))
	{
		define('PATH', './');
	}
	 	
	// there is a config
	require (PATH . 'includes/config.php');
		
	//include files .. & classes ..
	$path = dirname(__file__) . '/';
	$root_path = PATH;

	require ($path . 'mysql.php');
	require ($path . 'style.php');
	require ($path . 'functions.php');
	
	// start classes ..
	$SQL	= new SSQL($dbserver, $dbuser, $dbpass, $dbname);
	
	//no need after now 
	unset($dbpass);
	$tpl	= new phpquran_style;
	//$kljup	= new KljUploader;
	
	if (file_exists($root_path . 'cache/data_settings.php'))
	{
		include_once ($root_path . 'cache/data_settings.php');
	}
	//else
	//{
	//	big_error('Couldnt find phpquran settings file !!','phpquran is missing important file located in cache/data_settings.php');
	//}
	
	//getting cache [KLEEJA CACHE SYSTEM]
	if (empty($config) or !file_exists($root_path . 'cache/data_settings.php'))
	{
		$query = array(
					'SELECT'	=> 'c.*',
					'FROM'		=> "{$dbprefix}config c"
					);
									
		$result = $SQL->build($query); //start query
			
		$file_datac = '<' . '?php' . "\n\n";
		$file_datac .= "\n// auto-generated cache files\n// \n\n";
		$file_datac .= '$config = array( ' . "\n";

		while($row=$SQL->fetch_array($result))
		{
				$config[$row['name']] = $row['value'];
				$file_datac .= '\'' . $row['name'] . '\' => \'' . str_replace(array("'","\'"), "\'", $row['value']) . '\',' . "\n";
		}
		
		$file_datac .= ');' . "\n\n";
		$file_datac .= '?' . '>';
				
	 	$SQL->freeresult($result); //end query

		$filenumc = @fopen($root_path . 'cache/data_settings.php', 'w');
		@flock($filenumc, LOCK_EX); // exlusive look
		@fwrite($filenumc, $file_datac);
		@fclose($filenumc);
	}
	
	//select language from user-agent
	$lg = addslashes(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
	
	if(isset($lg))
	{
		if (file_exists($root_path . 'includes/langs/' . $lg . '.php'))
		{
			require ($root_path . 'includes/langs/' . $lg . '.php');
		}
		else
		{
			require ($root_path . 'includes/langs/ar.php');
		}
	}
	else
	{
		require ($root_path . 'includes/langs/ar.php');
	}
	
	
	
	if(defined('DEV_STAGE'))
	{
		$tpl->caching = false;
	}
		
	//phpquran session id
	$session = $SQL->escape(session_id());

	// ...header ..  i like it ;)
	header('Content-type: text/html; charset=UTF-8');	
	header('Cache-Control: private, no-cache="set-cookie"');
	header('Expires: 0');
	header('Pragma: no-cache');	
		
	//check style
	if(!isset($config['style']) || empty($config['style']))
	{
		$config['style'] = 'default';
	}
		
	$STYLE_PATH = PATH . 'styles/' . $config['style'] . '/';
	$STYLE_PATH_ADMIN  = PATH . 'admin/admin_style/';
			
	//site close ..
	$login_page = '';
	if ($config['siteclose'] == '1' && !is_admin())
	{
		// Send a 503 HTTP response code to prevent search bots from indexing the maintenace message
		header('HTTP/1.1 503 Service Temporarily Unavailable');
		phpquran_info($config['closemsg'], $lang['SITE_CLOSED']);
	}
	
	$show_online = ($config['allow_online']==1) ? true : false ;
	//calculate  onlines ...  
	if ($config['allow_online'] == '1')
	{
		phpquranOnline();
		$visitornum		= $usersnum	=	0;
		//$show_online	= true;
		$OnlineNames	= array();
		$timeout		= 100; //second  
		$timeout2		= time()-$timeout;  
			
		$query = array(
						'SELECT'	=> 'DISTINCT(n.ip), n.username, n.agent',
						'FROM'		=> "{$dbprefix}online n",
						'WHERE'		=> "n.time > '$timeout2'"
				);

		
		$result	= $SQL->build($query); 
		
		while($row=$SQL->fetch_array($result))
		{	
				$visitornum++;
		} #while
		
	 	$SQL->freeresult($result);
		
		/*
		wanna increase your onlines counter ..you can from next line 
		 but you must know this is illegial method ... 
		*/
		$allnumbers = $visitornum;
	}
	
	// claculate for counter ..
	 // of course , its not printable function , its just for calculating :)
	//visit_stats();
	
	//check for page numbr
	if(empty($perpage) || intval($perpage) == 0)
	{
		$perpage = 10;
	}
	
	//site url must end with /
	if($config['siteurl'])
	{
		$config['siteurl'] = ($config['siteurl'][strlen($config['siteurl'])-1] != '/') ? $config['siteurl'] . '/' : $config['siteurl'];
	}
	
	//captch file 
	$captcha_file_path = $config['siteurl'] . 'includes/captcha.php';
	
	
#<-- EOF
